using Digitalmes.Application.Sys.Orgs;
using Digitalmes.Application.Sys.Orgs.Commands;
using Digitalmes.Application.Sys.Orgs.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 组织机构终结点。
/// </summary>
public sealed class OrgEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "org")
            .RequireAuthorization()
            .MapGet(GetOrgListAsync, "list")
            .MapGet(GetOrgTreeAsync, "tree")
            .MapPost(CreateOrgAsync)
            .MapPut(UpdateOrgAsync, "{id}")
            .MapDelete(DeleteOrgAsync, "{id}");
    }

    [EndpointDescription("查询组织部门集合")]
    public async Task<IApiResult> GetOrgListAsync(ISender sender, [AsParameters] GetOrgListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("按树结构查询组织部门集合")]
    public async Task<IApiResult> GetOrgTreeAsync(ISender sender, [AsParameters] GetOrgListWithTreeQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建组织部门")]
    public async Task<IApiResult> CreateOrgAsync(ISender sender, SysOrgDto dto)
    {
        var result = await sender.Send(new CreateOrgCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新组织部门")]
    public async Task<IApiResult> UpdateOrgAsync(ISender sender, long id, SysOrgDto dto)
    {
        var result = await sender.Send(new UpdateOrgCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除组织部门")]
    public async Task<IApiResult> DeleteOrgAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteOrgCommand(id));
        return result.ToApiResult();
    }
}
